##   yatce
##   Copyright (C) 2009   UENISHI Kota <kuenishi+yatce@gmail.com>
##
##   This program is free software: you can redistribute it and/or modify
##   it under the terms of the GNU Lesser General Public License as published by
##   the Free Software Foundation, either version 3 of the License, or
##   (at your option) any later version.
##
##   This program is distributed in the hope that it will be useful,
##   but WITHOUT ANY WARRANTY; without even the implied warranty of
##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##   GNU Lesser General Public License for more details.
##
##   You should have received a copy of the GNU Lesser General Public License
##   along with this program.  If not, see <http://www.gnu.org/licenses/>.

EI_HOME = $(shell erl -noshell -eval 'io:format("~s~n", [code:lib_dir(erl_interface)]).' -s init stop)
ERL_DRIVER_H = -I$(shell erl -noshell -eval 'io:format("~s~n", [filename:absname("usr/include", code:root_dir())]).' -s init stop)

LIBEI= -L$(EI_HOME)/lib -lei_st #LIBEI=-lerl_interface_st # doesn't work: liberl_interface.a is deprecated.
EI_INCLUDE=-I$(EI_HOME)/include

TARGET=libyatce.so
OBJECTS=dispatcher.o tc_wrapper.o endecode.o easy_test.o \
tc_table_operation.o \
linked_list.o lock.o hash_map.o

CFLAGS += -fPIC -Wall -O2 #-O0 -DDEBUG_MSG #-DDEBUG_DONE

ifeq ($(shell uname),Linux)
	ARCH = linux
	SHAREFLAG = -shared
	LIBTC= -ltokyocabinet
	USR_LOCAL=/usr/local
else
	ARCH = Darwin
	CFLAGS += -bundle -undefined suppress -flat_namespace
	SHAREFLAG = -dynamic
	USR_LOCAL=/opt/local
endif

LIBTC= -L$(USR_LOCAL)/lib -ltokyocabinet
TC_INCLUDE=-I$(USR_LOCAL)/include

LIBRARY=$(LIBEI) $(LIBTC) -lz -lpthread -lm
INCLUDE=$(EI_INCLUDE) $(ERL_DRIVER_H) $(TC_INCLUDE)

all: $(TARGET) raw_performer 

.c.o:
	$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@


$(TARGET): $(OBJECTS) 
	$(CC) $(CFLAGS) $(SHAREFLAG) $(INCLUDE) $(OBJECTS) -o $(TARGET) $(LDFLAGS) $(LIBRARY)

clean:
	-rm -rf $(OBJECTS) $(BEAMS) $(TARGET) *.o

raw_performer: raw_performer.o
	$(CC) $(INCLUDE) $< -o $@ $(LIBRARY) 
